package it.niedermann.android.markdown.markwon.glide

import androidx.annotation.Px
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy

/**
 * @see [Source](https://github.com/noties/Markwon/issues/329.issuecomment-855220315)
 */
class DownsampleWithMaxWidth(@field:Px @param:Px private val maxWidth: Int) : DownsampleStrategy() {
    override fun getScaleFactor(
        sourceWidth: Int,
        sourceHeight: Int,
        requestedWidth: Int,
        requestedHeight: Int
    ): Float {
        // do not scale down if fits requested dimension
        return if (sourceWidth < maxWidth) {
            1f
        } else maxWidth.toFloat() / sourceWidth
    }

    override fun getSampleSizeRounding(
        sourceWidth: Int,
        sourceHeight: Int,
        requestedWidth: Int,
        requestedHeight: Int
    ): SampleSizeRounding {
        // go figure
        return SampleSizeRounding.MEMORY
    }
}